package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.suffix;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ExtMyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.SuffixUtil;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ExtUpdateByPrimaryKeySelectiveElementGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByPrimaryKeySelectiveElementGenerator;

import java.util.ArrayList;
import java.util.List;

/*********************************************************
 * 文件名称：SuffixUpdateByPrimaryKeySelectiveElementGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.suffix
 * 功能说明：带后缀的、主键、不为空、修改、XML生成器
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/30 17:25
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SuffixUpdateByPrimaryKeySelectiveElementGenerator extends ExtUpdateByPrimaryKeySelectiveElementGenerator {


    @Override
    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("update"); //$NON-NLS-1$

        answer.addAttribute(new Attribute(
                "id", SuffixUtil.getSuffixMethodName(introspectedTable.getUpdateByPrimaryKeySelectiveStatementId(),introspectedTable))); //$NON-NLS-1$

        String parameterType;

        if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
            parameterType = introspectedTable.getRecordWithBLOBsType();
        } else {
            parameterType = introspectedTable.getBaseRecordType();
        }

        answer.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-1$

        context.getCommentGenerator().addComment(answer);

        StringBuilder sb = new StringBuilder();

        sb.append("update "); //$NON-NLS-1$
        sb.append(SuffixUtil.addTableNameSuffix(introspectedTable.getFullyQualifiedTableNameAtRuntime(),introspectedTable));
        answer.addElement(new TextElement(sb.toString()));

        XmlElement dynamicElement = new XmlElement("set"); //$NON-NLS-1$
        answer.addElement(dynamicElement);

        for (IntrospectedColumn introspectedColumn :
                ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns())) {
            if (ExtMyBatis3FormattingUtilities.createSysdate(introspectedColumn)) {
                continue;
            }
            if (ExtMyBatis3FormattingUtilities.updateSysdate(introspectedColumn)) {
                sb.setLength(0);
                sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
                sb.append(" = ${getDBDate},");
                dynamicElement.addElement(new TextElement(sb.toString()));
                continue;
            }
            sb.setLength(0);
            sb.append(introspectedColumn.getJavaProperty("row."));
            sb.append(" != null"); //$NON-NLS-1$
            XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
            isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
            dynamicElement.addElement(isNotNullElement);

            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
            sb.append(" = "); //$NON-NLS-1$
            sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "row."));
            sb.append(',');

            isNotNullElement.addElement(new TextElement(sb.toString()));


        }

        buildPrimaryKeyWhereClause().forEach(answer::addElement);

        if (context.getPlugins().sqlMapUpdateByPrimaryKeySelectiveElementGenerated(answer, introspectedTable)
                && SuffixUtil.isEnableSuffix(introspectedTable)) {
            SuffixUtil.deleteOldElement(parentElement,answer);
            parentElement.addElement(answer);
        }
    }
    protected List<TextElement> buildPrimaryKeyWhereClause() {
        List<TextElement> answer = new ArrayList<>();
        boolean first = true;
        for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
            String line;
            if (first) {
                line = "where "; //$NON-NLS-1$
                first = false;
            } else {
                line = "  and "; //$NON-NLS-1$
            }

            line += MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn);
            line += " = "; //$NON-NLS-1$
            line += MyBatis3FormattingUtilities.getParameterClause(introspectedColumn,"row.");
            answer.add(new TextElement(line));
        }

        return answer;
    }
}
